﻿using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace CommonLibrary.UserControls
{
    public enum MoveButtonAction
    {
        Move,
        Copy
    }

    public partial class AddRemoveColumns : UserControl
    {
        #region Constants

        private const int DEFAULT_WIDTH = 200;
        private const int DEFAULT_HEIGHT = 300;
        private int _columnHeight = DEFAULT_HEIGHT;
        private int _columnWidth = DEFAULT_WIDTH;

        #endregion

        public AddRemoveColumns()
        {
            InitializeComponent();
            base.Resize += new EventHandler(AddRemoveColumns_SizeChanged);
        }

        void AddRemoveColumns_SizeChanged(object sender, EventArgs e)
        {
            UpdateChildControls();
        }

        #region Private Methods

        /// <summary>
        /// Move the selected items from one list to the other
        /// </summary>
        /// <param name="fromList"></param>
        /// <param name="toList"></param>
        /// <param name="action"></param>
        private void MoveItems(ListBox fromList, ListBox toList, MoveButtonAction action)
        {
            int itemCount = fromList.SelectedItems.Count;
            int selectedIndex = fromList.SelectedIndex;

            foreach (object item in fromList.SelectedItems)
            {
                if (toList.SelectedIndex >= 0)
                {
                    toList.Items.Insert(toList.SelectedIndex, item);
                }
                else
                {
                    toList.Items.Add(item);
                }
            }

            if (action == MoveButtonAction.Move)
            {
                for (int i = 0; i < itemCount; i++)
                {
                    object item = fromList.SelectedItems[0];
                    fromList.Items.Remove(item);
                }
            }

            if (fromList.Items.Count > 0)
            {
                fromList.ClearSelected();
                if (selectedIndex > 0)
                {
                    fromList.SelectedIndex = selectedIndex - 1;
                }
                else
                {
                    fromList.SelectedIndex = 0;
                }
            }
        }

        private void UpdateControls()
        {
            base.SetBounds(base.Left, base.Top, (ColumnWidth + 50) * 2, ColumnHeight + 40, BoundsSpecified.Size);
            UpdateChildControls();
        }

        private void UpdateChildControls()
        {
            Listbox1.SetBounds(Listbox1.Left, Listbox1.Top, ColumnWidth, ColumnHeight, BoundsSpecified.Size);
            Listbox2.SetBounds((this.Width - ColumnWidth - 20), Listbox2.Top, ColumnWidth, ColumnHeight, BoundsSpecified.All);
            ButtonsPanel.SetBounds(Listbox1.Width + 30, (Listbox1.Height / 2) - 30, ButtonsPanel.Height, ButtonsPanel.Width, BoundsSpecified.Location);            
        }

        #endregion

        #region Properties

        [Category("Behavior")]
        public MoveButtonAction MoveLeftAction { get; set; }

        [Category("Behavior")]
        public MoveButtonAction MoveRightAction { get; set; }

        [Category("Layout")]
        public int ColumnHeight 
        {
            get { return _columnHeight; }
            set
            {
                _columnHeight = value;
                UpdateControls();
            }
        }

        [Category("Layout")]
        public int ColumnWidth 
        {
            get { return _columnWidth; }
            set
            {
                _columnWidth = value;
                UpdateControls();
            }
        }

        [Category("Data"), Description("The items in the left list")]
        public ListBox.ObjectCollection LeftList 
        {
            get { return Listbox1.Items; }
            set 
            {
                Listbox1.Items.Clear();
                Listbox1.Items.AddRange(value);
            }
        }

        [Category("Data"), Description("The items in the right list")]
        public ListBox.ObjectCollection RightList 
        {
            get { return Listbox2.Items; }
            set
            {
                Listbox2.Items.Clear();
                Listbox2.Items.AddRange(value);
            }
        }

        [Category("Layout")]
        public new int Width
        {
            get { return base.Width; }
            set
            {
                ColumnWidth = (value / 2) - 50; 
            }
        }

        [Category("Layout")]
        public new int Height
        {
            get { return base.Height; }
            set
            {
                ColumnHeight = value - 40;
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Set the display values based on the passed in values
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddRemoveColumns_Load(object sender, EventArgs e)
        {
            UpdateControls();
        }

        private void MoveRightButton_Click(object sender, EventArgs e)
        {
            MoveItems(Listbox1, Listbox2, MoveRightAction);
        }

        private void MoveLeftButton_Click(object sender, EventArgs e)
        {
            MoveItems(Listbox2, Listbox1, MoveLeftAction);
        }

        private void Listbox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            MoveLeftButton.Enabled = (Listbox2.SelectedIndex >= 0);
        }

        private void Listbox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MoveRightButton.Enabled = (Listbox1.SelectedIndex >= 0);
        }

        #endregion
    }
}
